<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-us" lang="en-us">
<head>
  <link href="//gmpg.org/xfn/11" rel="profile">
  <meta http-equiv="content-type" content="text/html; charset=utf-8">
  <meta name="generator" content="Hugo 0.69.0" />

  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">

  <title>PIL图片添加文字 &middot; 郝朝帅的博客</title>

  
  <link type="text/css" rel="stylesheet" href="/blog/css/print.css" media="print">
  <link type="text/css" rel="stylesheet" href="/blog/css/poole.css">
  <link type="text/css" rel="stylesheet" href="/blog/css/syntax.css">
  <link type="text/css" rel="stylesheet" href="/blog/css/hyde.css">
    <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Abril+Fatface|PT+Sans:400,400i,700">


  
  <link rel="apple-touch-icon-precomposed" sizes="144x144" href="/apple-touch-icon-144-precomposed.png">
  <link rel="shortcut icon" href="/favicon.png">

  
  
</head>

  <body class="theme-base-0f ">
  <aside class="sidebar">
  <div class="container sidebar-sticky">
    <div class="sidebar-about">
      <a href="/blog/"><h1>郝朝帅的博客</h1></a>
      <p class="lead">
       我的个人博客 
      </p>
    </div>

    <nav>
      <ul class="sidebar-nav">
        <li><a href="/blog/">Home</a> </li>
        
      </ul>
    </nav>

    <p>&copy; 2020. All rights reserved. </p>
  </div>
</aside>

    <main class="content container">
    <div class="post">
  <h1>PIL图片添加文字</h1>
  <time datetime=2020-04-29T21:39:26&#43;0800 class="post-date">Wed, Apr 29, 2020</time>
  <p>opencv是一个强大的图像处理和计算机视觉库</p>
<p>安装的时候是 opencv_python，但在导入的时候采用 import cv2</p>
<p>安装
pip install opencv-python</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-python" data-lang="python">
<span style="color:#75715e"># #图像处理模块</span>
<span style="color:#f92672">import</span> cv2


<span style="color:#75715e"># #读图</span>
<span style="color:#75715e"># img = cv2.imread(&#39;./code.png&#39;,cv2.IMREAD_GRAYSCALE)</span>
<span style="color:#75715e">#</span>
<span style="color:#75715e">#</span>
<span style="color:#75715e"># #写图</span>
<span style="color:#75715e"># cv2.imwrite(&#39;./code1.png&#39;,img)</span>




<span style="color:#75715e">#图像压缩</span>
<span style="color:#75715e">#读图</span>
img <span style="color:#f92672">=</span> cv2<span style="color:#f92672">.</span>imread(<span style="color:#e6db74">&#39;./timg2.png&#39;</span>)


<span style="color:#75715e"># #压缩  0-9</span>
<span style="color:#75715e"># a=cv2.imwrite(&#39;./timg2.png&#39;,img,[cv2.IMWRITE_PNG_COMPRESSION,9])</span>
<span style="color:#75715e"># jpg是有损压缩，png是无损压缩</span>
<span style="color:#75715e"># cv2.imwrite(&#39;./timg2.png&#39;,img,[cv2.IMWRITE_PNG_COMPRESSION,9])</span>
<span style="color:#75715e">#[cv2.IMWRITE_PNG_COMPRESSION,0]是调整PNG图像的压缩比。</span>
<span style="color:#75715e"># 为0时，压缩比最小，图像的品质最好。它的压缩范围为[0，9]</span>
<span style="color:#75715e">#</span>
<span style="color:#75715e"># #jpg 0-100</span>
cv2<span style="color:#f92672">.</span>imwrite(<span style="color:#e6db74">&#39;./timg2.png&#39;</span>,img,[cv2<span style="color:#f92672">.</span>IMWRITE_JPEG_QUALITY,<span style="color:#ae81ff">0</span>])
<span style="color:#e6db74">&#39;&#39;&#39;
</span><span style="color:#e6db74">[cv2.IMWRITE_JPEG_QUAITY,50]可以实现图像的压缩。
</span><span style="color:#e6db74">
</span><span style="color:#e6db74">其中，50是可以改变的，取值为[0,100]
</span><span style="color:#e6db74">
</span><span style="color:#e6db74">0时图像可以得到极大地压缩，但是图像的品质会被大大降低。
</span><span style="color:#e6db74">
</span><span style="color:#e6db74"> 
</span><span style="color:#e6db74">&#39;&#39;&#39;</span>


</code></pre></div>
</div>


    </main>

    
  </body>
</html>
